package com.etc.controller;

import com.etc.entity.CommentResult;
import com.etc.entity.Room;
import com.etc.service.BuildingService;
import com.etc.service.OwnerService;
import com.etc.service.ParkService;
import com.etc.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RoomController {
    @Autowired
    private RoomService roomService;

    @Autowired
    private ParkService parkService;

    @Autowired
    private BuildingService buildingService;

    @Autowired
    private OwnerService ownerService;

    @RequestMapping("room/findAllRoom")
    public CommentResult findAllRoom(@RequestParam(value = "start", required = false) Integer start,
                                     @RequestParam(value = "size", required = false) Integer size)
    {
        List<Room> list = roomService.findAllRoom();
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());

            map.put("room_num",list.get(i).getRoom_num());
            map.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map.put("owner_name", null);
                map.put("owner_id",null);
            }
            else {
                map.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map);

        }
        return new CommentResult(200,"success",result);
    }



    @RequestMapping("room/findById/{id}")
    public CommentResult findById(@PathVariable("id") Integer id) {

        Room result = roomService.findById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("id",result.getId());
        map.put("park_name",parkService.findById(result.getPark_id()).getPark_name());
        map.put("park_id",parkService.findById(result.getPark_id()).getId());
        map.put("building_id",buildingService.findById(result.getBuilding_id()).getId());
        map.put("building_name",buildingService.findById(result.getBuilding_id()).getName());
        map.put("room_num",result.getRoom_num());
        if (result.getOwner_id()==null) {
            map.put("owner_name", null);
            map.put("owner_id",null);
        }
        else {
            map.put("owner_name", ownerService.findById(result.getOwner_id()).getName());
            map.put("owner_id", ownerService.findById(result.getOwner_id()).getId());
        }
        map.put("area",result.getArea());

        return new CommentResult(200,"success",map);
    }


    @RequestMapping("room/findByPark/{id}")
    public CommentResult findByPark(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                    @RequestParam(value = "size", required = false) Integer size)
    {
        List<Room> list = roomService.findByPark(id);
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("room_num",list.get(i).getRoom_num());
            map.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map.put("owner_name", null);
                map.put("owner_id",null);
            }
            else {
                map.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map);

        }
        return new CommentResult(200,"success",result);
    }

    @RequestMapping("room/findByBuilding/{id}")
    public CommentResult findByBuilding(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                        @RequestParam(value = "size", required = false) Integer size)
    {
        List<Room> list = roomService.findByBuilding(id);
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("room_num",list.get(i).getRoom_num());
            map.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map.put("owner_name", null);
                map.put("owner_id",null);
            }
            else {
                map.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map);

        }
        return new CommentResult(200,"success",result);
    }


    @RequestMapping("room/findByOwner/{id}")
    public CommentResult findByOwner(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                     @RequestParam(value = "size", required = false) Integer size)
    {
        List<Room> list = roomService.findByOwner(id);
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("room_num",list.get(i).getRoom_num());
            map.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map.put("owner_name", null);
                map.put("owner_id",null);
            }
            else {
                map.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map);

        }
        return new CommentResult(200,"success",result);
    }


    @RequestMapping("room/findByRoomNum/{id}")
    public CommentResult findByRoomNum(@PathVariable("id")Integer id,@RequestParam(value = "start", required = false) Integer start,
                                       @RequestParam(value = "size", required = false) Integer size)
    {
        List<Room> list = roomService.findByRoomNum(id);
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("room_num",list.get(i).getRoom_num());
            map.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map.put("owner_name", null);
                map.put("owner_id",null);
            }
            else {
                map.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map);

        }
        return new CommentResult(200,"success",result);
    }
    @RequestMapping("room/findByAnyCondition")
    public CommentResult findByAnyCondition(@RequestBody Map<String, Object> map, @RequestParam(value = "start", required = false) Integer start,
                                            @RequestParam(value = "size", required = false) Integer size)
    {
        List<Room> list = roomService.findByAnyCondition(map);
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map1 = new HashMap<>();
            map1.put("id",list.get(i).getId());
            map1.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map1.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map1.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map1.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map1.put("room_num",list.get(i).getRoom_num());
            map1.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map1.put("owner_name", null);
                map1.put("owner_id",null);
            }
            else {
                map1.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map1.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map1);

        }
        return new CommentResult(200,"success",result);
    }
    @RequestMapping("room/findFreeRoom")
    public CommentResult findFreeRoom(@RequestParam(value = "start", required = false) Integer start,
                                      @RequestParam(value = "size", required = false) Integer size)
    {
        List<Room> list = roomService.findFreeRoom();
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("room_num",list.get(i).getRoom_num());
            map.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map.put("owner_name", null);
                map.put("owner_id",null);
            }
            else {
                map.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map);

        }
        return new CommentResult(200,"success",result);
    }

    @RequestMapping("room/findFreeRoomByBuilding/{id}")
    public CommentResult findFreeRoomByBuilding(@PathVariable("id")Integer id)
    {
        List<Room> list = roomService.findFreeRoomByBuilding(id);
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("room_num",list.get(i).getRoom_num());
            map.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map.put("owner_name", null);
                map.put("owner_id",null);
            }
            else {
                map.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map);

        }
        return new CommentResult(200,"success",result);
    }

    @RequestMapping("room/findBusyRoom")
    public CommentResult findBusyRoom(){
        List<Room> list = roomService.findBusyRoom();
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("room_num",list.get(i).getRoom_num());
            map.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map.put("owner_name", null);
                map.put("owner_id",null);
            }
            else {
                map.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map);

        }
        return new CommentResult(200,"success",result);
    }

    @RequestMapping("room/findBusyRoomByBuilding/{id}")
    public CommentResult findBusyRoomByBuilding(@PathVariable("id") Integer id)
    {
        List<Room> list = roomService.findBusyRoomByBuilding(id);
        List<Map<String,Object>> result = new ArrayList<>();

        for (int i = 0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i).getId());
            map.put("park_name",parkService.findById(list.get(i).getPark_id()).getPark_name());
            map.put("park_id",parkService.findById(list.get(i).getPark_id()).getId());
            map.put("building_id",buildingService.findById(list.get(i).getBuilding_id()).getId());
            map.put("building_name",buildingService.findById(list.get(i).getBuilding_id()).getName());
            map.put("room_num",list.get(i).getRoom_num());
            map.put("area",list.get(i).getArea());
            if (list.get(i).getOwner_id()==null) {
                map.put("owner_name", null);
                map.put("owner_id",null);
            }
            else {
                map.put("owner_name", ownerService.findById(list.get(i).getOwner_id()).getName());
                map.put("owner_id", ownerService.findById(list.get(i).getOwner_id()).getId());
            }
            result.add(map);

        }
        return new CommentResult(200,"success",result);
    }

}
